package com.chenyue.cm.user.controller;


import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.moudules.system.utils.ShiroUtils;
import com.chenyue.cm.user.domain.JdAttrValue;
import com.chenyue.cm.user.domain.JdAttrValueExample;
import com.chenyue.cm.user.service.JdAttrValueService;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
* @author chenyue-cm
* @date 2024-03-13
*/
@RestController
@RequestMapping("/jdAttrValue")
public class JdAttrValueController {

    @Autowired
    private JdAttrValueService mAttrValueService;


    @RequestMapping("/getList")
    @ResponseBody
    @Log(value="获取商品sku属性值列表",type="2",module="2")
    public Success<PageVo<JdAttrValue>> getList(HttpServletRequest request, HttpServletResponse response) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdAttrValue> result  = mAttrValueService.getPageBean(pageInfo,JdAttrValue.class);
        return Success.success(result);
    }


    /**
     * @param vo
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    @Log(value="保存商品sku属性值",type="2",module="2")
    public Success save(JdAttrValue vo, HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");

        if (id != null && id != "") {
            vo.setUpdateTime(DateUtils.getTimeNow());
            vo.setUpdateUser(ShiroUtils.getUserId());
            mAttrValueService.getBaseMapper().updateById(vo);
        } else {
            vo.setId(UUID.randomUUID().toString().replace("-", ""));
            vo.setCreateTime(DateUtils.getTimeNow());
            vo.setCreateUser(ShiroUtils.getUserId());
            mAttrValueService.save(vo);
        }

        return Success.success();
    }


    /**
     *
     * @param id
     * @param request
     * @param response
     * @return
     */
    @Log(value="根据id删除商品sku属性值",type="2")
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Success delete(@PathVariable String id, HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");

        JdAttrValueExample aExample = new JdAttrValueExample();
        aExample.createCriteria().andIdEqualTo(id);
        mAttrValueService.deleteByExample(aExample);

        return Success.success();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Log(value="商品sku属性值 -删除多个数据",type="2",module="2")
    public Success delete(HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");
        String ids =  request.getParameter("ids");
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            mAttrValueService.deleteByPrimaryKey(idString);
        }
        return Success.success();
    }

    /**
     * 获取value值
     */
    @RequestMapping("/getAttrValueListByKeyIds")
    @ResponseBody
    public Success<List<JdAttrValue>> getAttrValueListByKeyIds(HttpServletRequest request, HttpServletResponse response) {
        String keyIds = request.getParameter("keyIds");
        String[] split = keyIds.split(",");
        List<JdAttrValue> mAttrValueByKeyIds = mAttrValueService.getMAttrValueByKeyIds(Arrays.asList(split));
        return Success.success(mAttrValueByKeyIds);
    }

}
